package com.tanhua.server.controller;

import com.tanhua.domain.vo.PageResult;

import com.tanhua.server.service.CommentService;

import com.tanhua.server.service.MqService;


import com.tanhua.server.service.VideoService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

import java.util.Map;

@RestController

@RequestMapping("/smallVideos")

public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private CommentService commentService;
    @Autowired
    private MqService mqService;

    @PostMapping(name="上传小视频")
    public ResponseEntity uploadVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws Exception{
        return videoService.uploadVideo(videoThumbnail,videoFile);
    }
    @GetMapping(name="分页查询小视频")
    public ResponseEntity queryVideo(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10")Integer pagesize) throws Exception{
        PageResult pageResult = videoService.queryVideo(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping(value = "/{id}/like",name = "对小视频点赞")
    public ResponseEntity like(@PathVariable("id") String videoId) {
        mqService.sendMsgToMq(videoId,2);
        return videoService.like(videoId);
    }

    @PostMapping(value = "/{id}/dislike",name = "对小视频取消点赞")
    public ResponseEntity dislike(@PathVariable("id") String videoId) {
        mqService.sendMsgToMq(videoId,4);
        return videoService.dislike(videoId);
    }

    @GetMapping(value = "/{id}/comments",name = "查询视频的评论列表")
    public ResponseEntity queryComments(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10")Integer pagesize,
                                        @PathVariable("id") String videoId){
        Integer commentType = 5;
        return commentService.queryComments(page,pagesize,videoId,commentType);
    }

    @PostMapping(value = "/{id}/comments",name = "对小视频评论")
    public ResponseEntity saveComments(@PathVariable("id") String videoId,@RequestBody Map<String,String> comments) {
        mqService.sendMsgToMq(videoId,3);
        String commentType = "5";
        String video = "1";
        Map<String, String> map = new HashMap<>();
        map.put("videoId", videoId);
        map.put("comment", comments.get("comment"));
        map.put("commentType", commentType);
        map.put("video", video);
        return commentService.saveComments(map);
    }

}
